package com.project.service.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.service.BaseService;
import com.project.dao.sys.SysConstantDao;
import com.project.entity.sys.SysConstant;
import com.project.util.CurrentSessionUtil;

/**
 *  系统常量service
 * @author 
 *
 */
@Transactional
@Service
public class SysConstantService extends BaseService<SysConstant>{

	@Autowired
	private SysConstantDao sysConstantDao;
	
	@Autowired
	private SysLogService sysLogService;
	
	
	public void save(SysConstant sysConstant){
		sysConstant.setUpdateUser(CurrentSessionUtil.getCurrentUser().getUserCode());
		sysConstantDao.saveOrUpdate(sysConstant);
		
		/*日志*/
		sysLogService.save("update", "sysConstant", sysConstant.getId(), "系统常量管理-修改常量值", "修改常量值", "常量"+sysConstant.getName()+"值已修改");
	}
	
	public Map<String,String> getConstantMap(){
		List<SysConstant> list = sysConstantDao.query(new SysConstant());
		Map<String,String> map = new HashMap<>();
		for (SysConstant sysConstant : list) {
			map.put(sysConstant.getCode(), sysConstant.getValue());
		}
		return map;
	}

	public SysConstant getConstantByCode(String code) {
		SysConstant s = new SysConstant();
		s.setCode(code);
		return sysConstantDao.queryEntity(s);
	}
	
	public void updateValueByCode(String code,String value) {
		sysConstantDao.updateValueByCode(code,value);
	}

	public List<SysConstant> getConstantListByCode(String code) {
		SysConstant s = new SysConstant();
		s.setCode(code);
		return sysConstantDao.query(s);
	}
	
}
